﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System.Collections.Generic;
using Daidalos.Exception;

namespace Daidalos.Components
{

    public class Panel : Component
    {

        #region Constructors
        public Panel(string label, float left, float top, float width, float height)
            : base(label, left, top, width, height)
        {
        }
        #endregion

        #region Methods
        /// <summary>
        ///     This method overrides the display method of the
        ///     base class.
        /// </summary>
        override public void Display()
        {
            // Verify state.
            if (children == null)
            {
                throw new InvalidStateException("A child list was lost, resulting in a corrupted UI state!");
            }

            // Panels are invisible, therefore they don't paint themselves, but their children.
            foreach (Component child in children)
            {
                // Invoke the display method of the child.
                child.Display();
            }
        }

        /// <summary>
        ///     This method checks if the component is hit if an
        ///     action is performed at a certain position.
        /// </summary>
        /// <param name="x">
        ///     The x-position of the action.
        /// </param>
        /// <param name="y">
        ///     The y-position of the action.
        /// </param>
        /// <returns>
        ///     True, iff. the component is hit.
        /// </returns>
        /// <remarks>
        ///     The panel itself cannot be hit by default. So the
        ///     hitting is reduced to the hitting of the child
        ///     components.
        /// </remarks>
        override public Component GetTargetedComponent(float x, float y)
        {
            // Check if the hit is in the area of the panel.
            if (x > 0 && y > 0 && x < width && y < height)
            {
                // Delegate hit test to the children.
                foreach (Component child in children)
                {
                    // Perform hit test on children (assert that children are ordered
                    // by their z-position).
                    Component result = child.GetTargetedComponent(x - child.Left, y - child.Top);

                    // Did we hit something?
                    if (result != null)
                    {
                        return result;
                    }
                }
            }

            // Let's assume that nothing was hit.
            return null;
        } 
        #endregion

    }

}
